package light.head.controller.oa;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.oa.TaskDAO;
import light.head.dao.oa.TransferDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Task;
import light.head.model.Transfer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = Url.C_TASK)
public class TaskController extends SupporterController{
	
	private TaskDAO taskDAO;
	private TransferDAO transferDAO;
	private OrganDAO organDAO;
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.TASK_INIT);
	}

	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(String type, HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		
		if(type.equals(Sys.S_ONE)) {
			Map<String, String> map = createQueryMapByParmName(request, Parm.HUSER, Parm.OC, Parm.USER, Parm.START, Parm.DUSER, Parm.TITLE, Parm.END);
			map.put(Parm.USER, cu_code(request).toString());
			map.put(Parm.OC, cu_organCode(request).toString());
			if (taskDAO.findByMap(organDAO.getSonCodeList(cu_organCode(request)),map, pb) == RESULT.SUCCESS) {
				request.setAttribute(Parm.PAGE_BEAN, pb);
			}
			
			return new ModelAndView(Url.TASK_LIST);
		}else {
			Map<String, String> map = createQueryMapByParmName(request, Parm.TN, Parm.RN, Parm.TITLE, Parm.DELETED, Parm.CU, Parm.OC);
			map.put(Parm.OC, cu_organCode(request).toString());
			if (transferDAO.findByMap(map, pb) == RESULT.SUCCESS) {
				request.setAttribute(Parm.PAGE_BEAN, pb);
			}
			
			return new ModelAndView(Url.TASK_LISTTRANSFER);
		}
		
	}
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(null == cu_organCode(request)) {
			PageBean pb = createPageBean(request);
			request.setAttribute(Parm.PAGE_BEAN, pb);
			return new ModelAndView(Url.TASK_INIT);
		}
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			Task task = taskDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.TASK, task);
		}
		return new ModelAndView(Url.TASK_EDIT);
	}
	
	@RequestMapping(value = Url.EDIT_SHOW)
	public ModelAndView editShow(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if(null == cu_organCode(request)) {
			PageBean pb = createPageBean(request);
			request.setAttribute(Parm.PAGE_BEAN, pb);
			return new ModelAndView(Url.TASK_INIT);
		}
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			Task task = taskDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.TASK, task);
		}
		return new ModelAndView(Url.TASK_EDITSHOW);
	}
	
	@Transactional
	@RequestMapping(value = Url.SAVE)
	public void save(Task task, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if (null == task.getCode()) {
			if(task.getTitle().equals("")) {
				print(response, Msg.NULL_TITLE);
				return;
			}if(task.getContent().equals("")) {
				print(response, Msg.NOT_FIND);
				return;
			}if(task.getRu() == null) {
				print(response, Msg.NULL_USER);
				return;
			}if(task.getStart() == null || task.getEnd() == null) {
				print(response, Msg.NULL_TIME);
				return;
			}
			
			task.setDeleted(false);
			task.setUser(cu_code(request));
			task.setDu(cu_code(request));
			task.setOc(cu_organCode(request));
			result = taskDAO.add(task);
			
		} else {
			if(null != task.getFettle() && task.getFettle() == true){
				print(response, Msg.NOT_UPDATE);
				return;
			}
			result = taskDAO.update(task);
		}
		
		if (RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		} else 
			print(response, Msg.ERROR);
	}
	
	
	@RequestMapping(value = Url.DELETE)
	public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		Task task = taskDAO.get(Integer.parseInt(code));
		task.setDeleted(true);
		if (taskDAO.update(task) == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else 
			print(response, Msg.ERROR);
	}
	
	
//移交
	@RequestMapping(value = Url.TRANSFEREDIT)
	public ModelAndView transferEdit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		request.setAttribute(Parm.MC, code);
		Task task = taskDAO.get(Integer.parseInt(code));
		request.setAttribute(Parm.TASK, task);
		return new ModelAndView(Url.TASK_EDITTRANSFER);
	}
	@RequestMapping(value = Url.SAVETRANSFER)
	public void saveTransfer(Transfer tran, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if (null == tran.getCode()) {
			tran.setDeleted(false);
			tran.setCt(new Date());
			tran.setOc(cu_organCode(request));
			Task task = taskDAO.get(tran.getMc());
			task.setRu(tran.getrName());
			taskDAO.upd(task);
			result = transferDAO.add(tran);
		}
		if (RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		} else 
			print(response, Msg.ERROR);
	}
	@RequestMapping(value = Url.SHOWEDITTRANSFER)
	public ModelAndView showEditTransfer(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			Transfer tran = transferDAO.get(Integer.parseInt(code));
			Task task = taskDAO.get(tran.getMc());
			request.setAttribute(Parm.TRANSFER, tran);
			request.setAttribute(Parm.TASK, task);
		}
		return new ModelAndView(Url.TASK_SHOWEDITTRANSFER);
	}

	@RequestMapping(value = Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		Transfer tran = transferDAO.get(Integer.parseInt(code));
		if (null == tran) {
			print(response, Msg.NOT_FIND);
			return;
		}

		tran.setDeleted(true);
		if (taskDAO.upd(tran) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		
		Transfer tran = transferDAO.get(Integer.parseInt(code));
		if (null == tran) {
			print(response, Msg.NOT_FIND);
			return;
		}
			
		tran.setDeleted(false);
		if (taskDAO.upd(tran) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	
	@Autowired
	public void setTaskDAO(TaskDAO taskDAO) {
		this.taskDAO = taskDAO;
	}
	@Autowired
	public void setTransferDAO(TransferDAO transferDAO) {
		this.transferDAO = transferDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
}
